<template>
    <dl>
        <dt>
            <img src="./截图20250818092514.png" alt="" width="200px">
        </dt>
        <dd>
            <div class="box">
                <h2>欢迎登录</h2>
                <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
                    <el-form-item  label="账号" prop="username">
                        <el-input v-model="ruleForm.username" />
                    </el-form-item>

                    <el-form-item label="密码" prop="password">
                        <el-input v-model="ruleForm.password" />
                        <div class="yzm">
                            <input type="text">
                            <button @click="dj">{{ time > 0 ? '剩余' + time + '秒' : '发送验证码' }}</button>
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="submitForm(ruleFormRef)">
                            登录
                        </el-button>


                    </el-form-item>
                </el-form>
            </div>
        </dd>
    </dl>


</template>

<script setup>
import { reactive, ref } from 'vue'
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: '',
})

const rules = reactive({
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
        { min: 3, max: 8, message: ' 3 to 8', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 8, message: ' 3 to 8', trigger: 'blur' },
    ],
})

import http from '@/api/http'
import { ElMessage } from 'element-plus'
import router from '@/router'
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            http.post('/login', ruleForm).then(res => {
                console.log('登录', res);
                if (res.data.code == 10000) {
                    ElMessage({
                        message: '登录成功',
                        type: 'success',
                    })
                    router.push('/index')
                    localStorage.setItem('accessToken', res.data.data.accessToken)
                    localStorage.setItem('refreshToken', res.data.data.refreshToken)
                    localStorage.setItem('avator', res.data.data.userInfo.avator)
                    localStorage.setItem('username', res.data.data.userInfo.username)
                }
                else {
                    ElMessage.error('登录失败')

                }

            })
        } else {
            ElMessage.error('登录')

        }
    })
}
// import { ref } from 'vue'
// let value = ref('');
// 
let time = 3
let timer;
let dj = () => {
    dj.value=Math.floor(Math.random()*10000)
    timer = setInterval(() => {
        time.value--
    }, 1000)
}
</script>

<style lang="scss" scoped>
.box {
    width: 300px;
    height: 300px;
    background-color: aqua;
    // margin-top: 300px;
    margin-left: 1000px;

    .el-button {
        width: 100px;
        margin-left: 50px;
    }

    h2 {
        margin-left: 80px;
        color: red;
    }
}

.dl {
    display: flex;
}
</style>